package com.nf.health.app.sweat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.http.Act;
import com.nf.health.app.sweat.bean.SweatSettingBean;
import com.nf.health.app.utils.ViewUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by he_pan on 2016/11/29.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author he_pan
 * @Description 设置汗液路数对应部位
 */

public class SweatSettingActivity extends BaseActivity {

    SweatSettingBean mSweatSetBean;//记录没路数据对应部位
    @Bind(R.id.et_position01)
    EditText etPosition01;
    @Bind(R.id.et_position02)
    EditText etPosition02;
    @Bind(R.id.et_position03)
    EditText etPosition03;
    @Bind(R.id.et_position04)
    EditText etPosition04;
    @Bind(R.id.et_position05)
    EditText etPosition05;
    @Bind(R.id.et_position06)
    EditText etPosition06;
    @Bind(R.id.et_position07)
    EditText etPosition07;
    @Bind(R.id.et_position08)
    EditText etPosition08;
    @Bind(R.id.et_position09)
    EditText etPosition09;
    @Bind(R.id.btn_save)
    Button btnSave;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_sweat_setting);
        ButterKnife.bind(this);
        initData();
        initViewAndListener();
    }

    private void initViewAndListener() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "汗液仪设置");
        btnSave.setOnClickListener(this);
    }

    private void initData() {
        provider.getUserSweatSetting();
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (Act.Flag.GET_SWEAT_SETTING_FLAG.equals(actionName)) {
            mSweatSetBean = (SweatSettingBean) object;
            if (mSweatSetBean != null) {
                setExistingData();
            }
        }else if (Act.Flag.UPDATE_SWEAT_SETTING_FLAG.equals(actionName)){
            Intent i = new Intent();
            i.putExtra(Constants.INTENT_EXTRA_BEAN,mSweatSetBean);
            setResult(RESULT_OK,i);
            finish();
        }
    }

    /**
     * 设置现有数据
     */
    private void setExistingData() {
        etPosition01.setText(mSweatSetBean.getSweaturl01());
        etPosition01.setSelection(etPosition01.getText().toString().length());
        etPosition02.setText(mSweatSetBean.getSweaturl02());
        etPosition03.setText(mSweatSetBean.getSweaturl03());
        etPosition04.setText(mSweatSetBean.getSweaturl04());
        etPosition05.setText(mSweatSetBean.getSweaturl05());
        etPosition06.setText(mSweatSetBean.getSweaturl06());
        etPosition07.setText(mSweatSetBean.getSweaturl07());
        etPosition08.setText(mSweatSetBean.getSweaturl08());
        etPosition09.setText(mSweatSetBean.getSweaturl09());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.btn_save){
            commitSetting();
        }
    }

    /**
     * 上传设置内容
     */
    private void commitSetting() {
        if (mSweatSetBean == null){
            mSweatSetBean = new SweatSettingBean();
        }
        mSweatSetBean.setSweaturl01(getText(etPosition01));
        mSweatSetBean.setSweaturl02(getText(etPosition02));
        mSweatSetBean.setSweaturl03(getText(etPosition03));
        mSweatSetBean.setSweaturl04(getText(etPosition04));
        mSweatSetBean.setSweaturl05(getText(etPosition05));
        mSweatSetBean.setSweaturl06(getText(etPosition06));
        mSweatSetBean.setSweaturl07(getText(etPosition07));
        mSweatSetBean.setSweaturl08(getText(etPosition08));
        mSweatSetBean.setSweaturl09(getText(etPosition09));

        provider.updateSweatSetting(mSweatSetBean);
    }


    private String getText(EditText et){
        return et.getText().toString().trim();
    }

}
